package com.service.impl;

import com.mapper.RoleMapper;
import com.pojo.Account;
import com.pojo.RightClassOne;
import com.pojo.Role;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 根据用户属性判断数据权限——查询session父级id是什么：
     * 0则不传，1则传roleId，其他直接返回null
     * 1.超级管理员，不需要传参
     * 2.主商户及子商户，查看自己子级角色，参数
     */
    public List<Role> showList(Account account) {
        if (account.getSuperId() == null) {
            account.setSuperId(null);
        } else if (account.getSuperId() == 0) {
            account.setSuperId(null);
        }
        return roleMapper.showList(account);
    }

    public Role showRoleMsg(Long id) {
        return roleMapper.showRoleMsg(id);
    }

    public boolean deleteRoleById(Integer id) {
        if (roleMapper.deleteRoleById(id) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 模糊查询
     *
     * @param role
     * @return
     */
    public List<Role> searchRoleByNameOrStatus(Role role) {
        return roleMapper.searchRoleByNameOrStatus(role);
    }

    /**
     * 添加商户
     *
     * @param role
     * @return
     */
    public boolean addBusiness(Role role) {
        if (role.getSuperId() == null) {
            role.setSuperId(1L);
        }
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        if (roleMapper.addBusiness(role) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询所有主商户
     *
     * @return
     */
    public List<Role> superBusinessList() {
        return roleMapper.superBusinessList();
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    public boolean updateBusiness(Role role) {
        role.setUpdateDate(new Date());  //修改时间
        if (roleMapper.updateBusiness(role) > 0) {
            return true;
        }
        return false;
    }


    public RightClassOne findRightsById(Integer id) {
        return roleMapper.findRightsById(id);
    }

    /**
     * 根据当前菜单编号绑定所有的后代菜单
     *
     * @param rights 保存所有的菜单
     * @param id
     * @return
     */
    public List<RightClassOne> findRightsBySuperId(List<RightClassOne> rights, Integer id) {
        //把当前父元素增加到集合
        if (id != null) {
            RightClassOne rightClassOne = roleMapper.findRightsById(id);
            if (rightClassOne != null) {
                rights.add(rightClassOne);
            }
        }
        //获取所有的子菜单
        List<RightClassOne> child = roleMapper.findRightsBySuperId(id);
        for (RightClassOne r : child) {
            //把当前的子菜单添加到集合中，并且也要把子菜单的子菜单添加到集合中
            findRightsBySuperId(rights, r.getRightId());
        }
        return rights;
    }

    public List<RightClassOne> findAllRights() {
        return roleMapper.findAllRights();
    }

    public List<RightClassOne> findAccountRights(Long id) {
        return roleMapper.findAccountRights(id);
    }

    public List<RightClassOne> findAllDoRights() {
        return roleMapper.findAllDoRights();
    }

    public List<RightClassOne> findAccountDoRights(Long roleId) {
        return roleMapper.findAccountDoRights(roleId);
    }

    public boolean updateBusinessChildsStatus(Role role) {
        if (roleMapper.updateBusinessChildsStatus(role) > 0) {
            return true;
        }
        return false;
    }

    public Role findAddRole(String roleName) {
        return roleMapper.findAddRole(roleName);
    }

    public boolean addRoleRights(String rights, Long addRoleId) {
        for (int i = 0; i < rights.split(",").length; i++) {
            System.out.println(rights.split(",")[i]);
            if (roleMapper.addRoleRights(rights.split(",")[i],addRoleId) < 0) {
                return false;
            }
        }
        return true;
    }

    public boolean deleteRoleRights(Long updateRoleId) {
        if(roleMapper.deleteRoleRights(updateRoleId)>0){
            return true;
        }
        return false;
    }
}
